package com.lanhao.aot.carrierintegration.gwe;

import java.io.File;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.beust.jcommander.internal.Maps;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.http.HttpRequest;

/**
 * Gwe公司的API对接，主要是英国皇家邮政
 * 
 * @author kunzhou3
 *
 */
public class GweCoreApiInvokeUtil {
	private static final Logger LOG = LoggerFactory.getLogger(GweCoreApiInvokeUtil.class);
	public static final String ORDER_CREATE_METHOD = "mps.order.create";
	public static final String TRACE_QUERY_METHOD = "mps.trace.query";
	public static final String LABEL_QUERY_METHOD = "mps.label.query";
	public static final String DEVICES = "1151142";
	public static final String CHANNEL = "CP0001";
	// 测试环境
//	public static final String API_URL = "http://60.205.176.122/order/api.do";
////	public static final String API_URL = "http://183.3.221.136:22235/order/api.do";
//	public static final String KEY = "247gcaqxlnvx38ivp07tt36pcskr6hf9";
	
	// 正式环境
	public static final String API_URL = "https://gwe.gw-express.com/order/api.do";
	public static final String KEY = "6njvwk4tx9z7gnuaspbzq1cwv9akg9mk";

	public static void main(String[] args) {
		JSONArray dataArray = new JSONArray();
		JSONObject requestData = new JSONObject();
		String cusOrderNo = IdUtil.getSnowflakeNextIdStr();
		requestData.put("cusOrderNo", cusOrderNo);
//		requestData.put("cusBillLadingNo", "Lnd20230621");
//		requestData.put("cusBoxNo", "Box20230621");
		requestData.put("productCode", CHANNEL);
		requestData.put("weightUnit", "kg");
		requestData.put("lengthUnit", "cm");
		requestData.put("packagingType", "other");
		requestData.put("syncGetLabel", "1");
		
		JSONObject senderInfo = new JSONObject();
		senderInfo.put("name", "Arpana Sunuwar");
//		senderInfo.put("company", "testSendCompany");
		senderInfo.put("tel", "07428405089");
//		senderInfo.put("mobilePhone", "07428405089");
//		senderInfo.put("email", "test@test.com");
		senderInfo.put("country", "GB");
		senderInfo.put("province", "England");
		senderInfo.put("city", "London");
//		senderInfo.put("district", "test1");
//		senderInfo.put("street", "test street1");
		senderInfo.put("zipCode", "W139QU");
		senderInfo.put("addressLine1", "189 Northfields Avenue");
//		senderInfo.put("addressLine2", "test send address2");
		requestData.put("senderInfo", senderInfo);

		JSONObject receiverInfo = new JSONObject();
		receiverInfo.put("name", "UK-RTN-AE");
//		receiverInfo.put("company", "-");
		receiverInfo.put("tel", "+447897159358");
//		receiverInfo.put("mobilePhone", "188866553112");
//		receiverInfo.put("email", "te2@test.com");
		receiverInfo.put("country", "GB");
		receiverInfo.put("province", "England");
		receiverInfo.put("city", "London");
//		receiverInfo.put("district", "test2");
//		receiverInfo.put("street", "test street2");
		receiverInfo.put("zipCode", "SE7 7RU");
		receiverInfo.put("addressLine1", "Unit 5, Devonshire Court, Victoria Road Feltham, Middlesex");
		receiverInfo.put("addressLine2", "");
		requestData.put("receiverInfo", receiverInfo);

		String parcelNumber = "PACK2023062115350001";
		
		JSONArray parcelListArray = new JSONArray();
		JSONObject parcelObject = new JSONObject();
		parcelObject.put("parcelNumber", parcelNumber);
		parcelObject.put("weight", 0.3);
//		parcelObject.put("volume", 1);
		parcelObject.put("length", 1);
		parcelObject.put("width", 1);
		parcelObject.put("height", 1);
//		parcelObject.put("remark", "");
		parcelObject.put("declaredValue", 0.01);
		parcelObject.put("declaredCurrency", "USD");
		parcelListArray.add(parcelObject);
		requestData.put("parcelList", parcelListArray);

		JSONArray itemListArray = new JSONArray();
		JSONObject itemObject = new JSONObject();
		itemObject.put("parcelNumber", parcelNumber);
		itemObject.put("itemName", "AnHeGoods");
		itemObject.put("sku", "220000000");
		itemObject.put("unitPrice", 0.01);
		itemObject.put("unitPriceCurrency", "USD");
		itemObject.put("quantity", 1);
		itemListArray.add(itemObject);
		requestData.put("itemList", itemListArray);
		
		dataArray.add(requestData);
		String data = dataArray.toString();
		String sign = Base64.encode(DigestUtil.md5Hex(data + KEY));
		
		Map<String, Object> formMap = Maps.newHashMap();
		formMap.put("method", ORDER_CREATE_METHOD);
		formMap.put("data", data);
		formMap.put("sign", sign);
		formMap.put("devices", DEVICES);
		formMap.put("v", "1.0");
		LOG.info("请求Gwe获取英国面单，url={}, 请求参数formMap={}", API_URL, formMap);
		String respBody = HttpRequest.post(API_URL).form(formMap).execute().body();
		LOG.info("请求Gwe获取英国面单，返回结果respBody={}", respBody);
		JSONObject respObject = JSON.parseObject(respBody);
		Integer code = respObject.getInteger("code");
		if (code == 200) {
			JSONArray dataList = respObject.getJSONArray("data");
			if (dataList != null && dataList.size() > 0) {
				JSONObject dataObject = dataList.getJSONObject(0);
				Boolean success = dataObject.getBoolean("success");
				if (success) {
					String carrierCode = dataObject.getString("carrierCode");
					JSONArray labelList = dataObject.getJSONArray("labelList");
					if (labelList != null && labelList.size() > 0) {
						JSONObject labelObject = labelList.getJSONObject(0);
						String trackingNumber = labelObject.getString("trackingNumber");
						String labelUrl = labelObject.getString("labelUrl");
						String labelContent = labelObject.getString("labelContent");
						LOG.info("获取Gwe标签成功carrierCode={}，trackingNumber={}，labelUrl={}", carrierCode, trackingNumber,
								labelUrl);
						Base64.decodeToFile(labelContent, new File("D:/" + trackingNumber + ".pdf"));
					}
				} else {
					String failCode = dataObject.getString("failCode");
					String failReason = dataObject.getString("failReason");
					LOG.info("调用Gwe获取面单失败，失败编码failCode={}，失败原因failReason={}", failCode, failReason);
				}
			}
		} else {
			String msg = respObject.getString("msg");
			LOG.info("调用Gwe获取面单失败，失败原因msg={}", msg);
		}
	}
}
